package com.andy.toast.extend

import android.graphics.drawable.Drawable
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.andy.base.AppHelper
import com.andy.toast.utils.ToastUtils

/**
 * Author:Andy
 * Date:2023/3/22 14:21
 * Description:Ext
 */
/**
 * 通过资源ID 获取颜色值
 */
fun Int.colorFromRes(): Int = ContextCompat.getColor(AppHelper.mContext, this)

/**
 * 通过资源ID 获取图片 Drawable
 */
fun Int.drawableFromRes(): Drawable? = ContextCompat.getDrawable(AppHelper.mContext, this)

/**
 * 通过资源ID 获取文字
 */
fun Int.stringFromRes(): String = AppHelper.mContext.getString(this)

/**
 * 显示普通提示
 *
 * @param time Int 时长，默认LENGTH_LONG
 */
fun Int.showNormalToast(time: Int = Toast.LENGTH_SHORT) {
    val text = AppHelper.mContext.getString(this)
    ToastUtils.normal(text, time)
}

/**
 * 显示成功提示
 *
 * @param time Int 时长，默认LENGTH_LONG
 */
fun Int.showSuccessToast(time: Int = Toast.LENGTH_SHORT) {
    val text = AppHelper.mContext.getString(this)
    ToastUtils.success(text, time)
}

/**
 * 显示错误提示
 *
 * @param time Int 时长，默认LENGTH_LONG
 */
fun Int.showErrorToast(time: Int = Toast.LENGTH_SHORT) {
    val text = AppHelper.mContext.getString(this)
    ToastUtils.error(text, time)
}

/**
 * 显示普通提示
 *
 * @param time Int 时长，默认LENGTH_LONG
 */
fun String.showNormalToast(time: Int = Toast.LENGTH_SHORT) {
    if (this.isNotEmpty()) {
        ToastUtils.normal(this, time)
    }
}

/**
 * 显示成功提示
 *
 * @param time Int 时长，默认LENGTH_LONG
 */
fun String.showSuccessToast(time: Int = Toast.LENGTH_SHORT) {
    if (this.isNotEmpty()) {
        ToastUtils.success(this, time)
    }
}

/**
 * 显示错误提示
 *
 * @param time Int 时长，默认LENGTH_LONG
 */
fun String.showErrorToast(time: Int = Toast.LENGTH_SHORT) {
    if (this.isNotEmpty()) {
        ToastUtils.error(this, time)
    }
}